{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial II: Linear Trotter steps of diagonal Coulomb operators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Electronic structure Hamiltonians with diagonal Coulomb operators\n",
    "\n",
    "When expressed in an arbitrary basis the molecular electronic structure Hamiltonian takes the form\n",
    "$$\n",
    "H = \\sum_{pq} h_{pq} a^\\dagger_p a_q + \\sum_{pqrs} h_{pqrs} a^\\dagger_p a^\\dagger_q a_r a_s\n",
    "$$\n",
    "where the coefficients $h_{pq}$ and $h_{pqrs}$ are determined by integrals taken over the basis functions. Note that this Hamiltonian has $O(N^4)$ terms which tends to make its simulation challenging on near-term devices.\n",
    "\n",
    "However, as discussed in [Phys. Rev. X 8, 011044](https://journals.aps.org/prx/abstract/10.1103/PhysRevX.8.011044), by carefully selection of basis function it is possible to obtain a representation that diagonalizes the Coulomb operator, leading to a much simpler Hamiltonian with $O(N^2)$ terms that can be written as\n",
    "$$\n",
    "H = \\sum_{pq} T_{pq} a^\\dagger_p a_q + \\sum_{pq} V_{pq} a^\\dagger_p a_p a^\\dagger_q a_q\n",
    "$$\n",
    "This form is derived in [Phys. Rev. X 8, 011044](https://journals.aps.org/prx/abstract/10.1103/PhysRevX.8.011044) by using basis functions that are related to a unitary rotation of plane waves. However, plane waves are not the only basis with this property; e.g., see [JCP 147, 244102](https://aip.scitation.org/doi/10.1063/1.5007066) for a basis that provides the diagonal form and high accuracy representation of single-molecules.\n",
    "\n",
    "Being a periodic basis, plane waves are particularly well suited to simulating periodic materials (e.g. solid state LiH instead of single molecule LiH in vacuum). One can use plane waves to also simulate single-molecules with a basis set discretization error that is asymptotically equivalent to Gaussian molecular orbitals; however, in practice for simulating single-molecules one often needs a constant factor more plane waves than Gaussians, and sometimes that constant factor is prohibitive for NISQ applications. In Tutorial III, we discuss how a combination of techniques from Tutorial I and this tutorial enable simulation of arbitrary basis electronic structure in low depth. However, this tutorial will focus on representations of the Hamiltonian with a diagonal Coulomb operator. The techniques discussed in this notebook are applicable to any molecular system, whether periodic or not. However, for simplicity this notebook will focus on the simulation of the uniform electron gas, aka \"jellium\". Jellium has the same Hamiltonian as an arbitrary molecule but without an external potential (i.e. $T_{pp}$ is uniform for all $p$)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Generation of a dual basis jellium Hamiltonian\n",
    "\n",
    "We begin by generating a small two-dimensional jellium model in the \"plane wave dual basis\" as in [Phys. Rev. X 8, 011044](https://journals.aps.org/prx/abstract/10.1103/PhysRevX.8.011044). Such two-dimensional jellium systems are often studied in the context of the fractional quantum Hall effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1256637061435917 [0^ 0] +\n",
      "-0.07957747154594769 [0^ 0 1^ 1] +\n",
      "-0.07957747154594769 [0^ 0 2^ 2] +\n",
      "-0.23873241463784306 [0^ 0 3^ 3] +\n",
      "-0.06283185307179587 [0^ 1] +\n",
      "-0.06283185307179585 [0^ 2] +\n",
      "-0.06283185307179587 [1^ 0] +\n",
      "0.1256637061435917 [1^ 1] +\n",
      "-0.07957747154594769 [1^ 1 0^ 0] +\n",
      "-0.23873241463784306 [1^ 1 2^ 2] +\n",
      "-0.07957747154594769 [1^ 1 3^ 3] +\n",
      "-0.06283185307179585 [1^ 3] +\n",
      "-0.06283185307179585 [2^ 0] +\n",
      "0.1256637061435917 [2^ 2] +\n",
      "-0.07957747154594769 [2^ 2 0^ 0] +\n",
      "-0.23873241463784306 [2^ 2 1^ 1] +\n",
      "-0.07957747154594769 [2^ 2 3^ 3] +\n",
      "-0.06283185307179587 [2^ 3] +\n",
      "-0.06283185307179585 [3^ 1] +\n",
      "-0.06283185307179587 [3^ 2] +\n",
      "0.1256637061435917 [3^ 3] +\n",
      "-0.23873241463784306 [3^ 3 0^ 0] +\n",
      "-0.07957747154594769 [3^ 3 1^ 1] +\n",
      "-0.07957747154594769 [3^ 3 2^ 2]\n"
     ]
    }
   ],
   "source": [
    "import openfermion\n",
    "\n",
    "# Set parameters of jellium model.\n",
    "wigner_seitz_radius = 5. # Radius per electron in Bohr radii.\n",
    "n_dimensions = 2 # Number of spatial dimensions.\n",
    "grid_length = 2 # Number of grid points in each dimension.\n",
    "spinless = True # Whether to include spin degree of freedom or not.\n",
    "n_electrons = 2 # Number of electrons.\n",
    "\n",
    "# Figure out length scale based on Wigner-Seitz radius and construct a basis grid.\n",
    "length_scale = openfermion.wigner_seitz_length_scale(\n",
    "    wigner_seitz_radius, n_electrons, n_dimensions)\n",
    "grid = openfermion.Grid(n_dimensions, grid_length, length_scale)\n",
    "\n",
    "# Initialize the model and print out.\n",
    "fermion_hamiltonian = openfermion.jellium_model(grid, spinless=spinless, plane_wave=False)\n",
    "print(fermion_hamiltonian)\n",
    "\n",
    "# Convert to DiagonalCoulombHamiltonian type.\n",
    "hamiltonian = openfermion.get_diagonal_coulomb_hamiltonian(fermion_hamiltonian)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the last line above we converted the FermionOperator to a class called DiagonalCoulombHamiltonian which is a special data structure in OpenFermion for representing operators that take the form\n",
    "$$\n",
    "H = \\sum_{pq} T_{pq} a^\\dagger_p a_q + \\sum_{pq} V_{pq} a^\\dagger_p a_p a^\\dagger_q a_q.\n",
    "$$\n",
    "OpenFermion-Cirq has implemented Hamiltonian simulation algorithms that are optimized specifically for Hamiltonians of this form. They take as input the OpenFermion data structure DiagonalCoulombHamiltonian, which represents such a Hamiltonian in terms of matrices storing $T_{pq}$ and $V_{pq}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initializing the mean-field state of jellium\n",
    "\n",
    "Often one would like to begin a simulation of electronic structure in the mean-field state. To do this while keeping operators in the dual basis one needs to apply a rotation of single particle basis functions (see Tutorial I). For arbitrary molecules this would necessitate first computing the canonical orbitals using a Hartree-Fock calculation, perhaps by using [OpenFermion-PySCF](https://github.com/quantumlib/OpenFermion-PySCF). However, since jellium has no external potential the mean-field state is an eigenstate of the one-body term $\\sum_{p, q} T_{pq} a^\\dagger_p a_q$. This term is a quadratic Hamiltonian, so its eigenstates can be prepared by applying a Bogoliubov transformation to a computational basis state. The Bogoliubov transformation changes the basis to one in which the quadratic Hamiltonian has the diagonal form $\\sum_{p} \\varepsilon_p b^\\dagger_p b_p$, where the $b^\\dagger_p$ are the creation operators for a new set of orbitals. We'll set the number of electrons to be half the total number of orbitals.\n",
    "\n",
    "We'll use the OpenFermion class QuadraticHamiltonian to obtain the Bogoliubov transformation matrix. Then, we'll initialize some qubits and create a circuit that applies the transformation to these qubits. Since our algorithms work with linear qubit connectivity, we'll use the `LineQubit` class. We won't bother compiling to Xmon gates for now to keep the circuits simple, but this can be done automatically using the appropriate Cirq methods. We will specify the initial state by passing in a list of the occupied orbitals (which in this case are just the first `n_electron` orbitals)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0: ──────────────YXXY─────────────────────────────────\n",
      "                 │\n",
      "1: ───YXXY───────#2^-0.502───────────────YXXY─────────\n",
      "      │                                  │\n",
      "2: ───#2^0.995───────────────YXXY────────#2^0.00482───\n",
      "                             │\n",
      "3: ──────────────────────────#2^-0.498────────────────\n"
     ]
    }
   ],
   "source": [
    "import cirq\n",
    "import openfermioncirq\n",
    "\n",
    "# Obtain the Bogoliubov transformation matrix.\n",
    "quadratic_hamiltonian = openfermion.QuadraticHamiltonian(hamiltonian.one_body)\n",
    "transformation_matrix = quadratic_hamiltonian.diagonalizing_bogoliubov_transform()\n",
    "\n",
    "# Create a circuit that prepares the mean-field state\n",
    "occupied_orbitals = range(n_electrons)\n",
    "n_qubits = openfermion.count_qubits(quadratic_hamiltonian)\n",
    "qubits = cirq.LineQubit.range(n_qubits)\n",
    "state_preparation_circuit = cirq.Circuit.from_ops(\n",
    "    openfermioncirq.bogoliubov_transform(\n",
    "        qubits, transformation_matrix, initial_state=occupied_orbitals))\n",
    "\n",
    "# Print circuit.\n",
    "cirq.DropNegligible().optimize_circuit(state_preparation_circuit)\n",
    "print(state_preparation_circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hamiltonian simulation via a Trotter-Suzuki product formula\n",
    "\n",
    "The goal of Hamiltonian time evolution simulation is to apply the unitary operator $\\exp(-i H t)$ for some time t. A simulation via a product formula proceeds by dividing the total evolution time $t$ into a finite number of steps $r$ and performing an approximate simulation of $\\exp(-i H t/r)$ $r$ times. Each simulation of $\\exp(-i H t/r)$ is called a Trotter step. The unitary $\\exp(-i H t/r)$ is approximated by interleaving simulations of the terms $H_j$ of a decomposition $H = \\sum_{j=1}^L H_j$. For example, the first-order symmetric, commonly known as the second-order, Trotter formula is\n",
    "$$\\exp(-i H t) \\approx \\prod_{j=1}^L \\exp(-i H_j t/2) \\prod_{j=L}^1 \\exp(-i H_j t/2).$$\n",
    "Higher-order product formulas are obtained from this one via a recursive construction. There is also a zeroth-order formula, which corresponds to\n",
    "$$\\exp(-i H t) \\approx \\prod_{j=1}^L \\exp(-i H_j t).$$\n",
    "In our case, the $H_j$ have the form $T_{pq} a^\\dagger_p a_q + T_{pq}^* a^\\dagger_q a_p$ or $V_{pq} a^\\dagger_p a_p a^\\dagger_q a_q$.\n",
    "\n",
    "To construct a circuit for performing time evolution via a product formula, we need to specify the total evolution time, the number of steps to use, and the order of the formula to use. For a fixed evolution time, increasing the number of steps and increasing the order of the formula both yield a more accurate simulation at the cost of increasing the gate count of the circuit. We could also specify an asymmetric Trotter step, or a controlled version, but we won't do that here. We will need to specify what algorithm will be used to compile the Trotter step. There are several options appropriate for DiagonalCoulombHamiltonians.\n",
    "\n",
    "A key result of [Phys. Rev. Lett. 120, 110501](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.120.110501) was to introduce a linear connectivity swap network which cycles through configurations in which all qubits are adjacent in at most $N$ parallel layers of swaps. As shown in that paper, by using fermionc swap gates instead of regular swap gates, one can use this swap network to simulate Trotter steps of an entire DiagonalCoulombHamiltonian. This effectively simulates $N / 2$ different pairs of terms $V_{pq} a^\\dagger_p a_p a^\\dagger_q a_q$ and $T_{pq} a^\\dagger_p a_q + T^*_{pq} a^\\dagger_q a_p$ in each layer of gates. By repeating the swap network for $N$ layers, all terms are simulated exactly once. This is referred to as the `LINEAR_SWAP_NETWORK` Trotter step in OpenFermion-Cirq.\n",
    "\n",
    "Another approach to implementing the Trotter step involves simulating all $\\sum_{pq} V_{pq} a^\\dagger_p a_p a^\\dagger_q a_q$ terms (which commute and thus, this involves no Trotter error) by using the linear swap networks (with normal swap gates), and then performing a basis transformation which diagonalizes the one-body terms so that all of the $\\sum_{pq} T_{pq} a^\\dagger_p a_q$ terms can be simulated at once, and then performing another basis transformation to restore the original basis. This method involves more gates in each Trotter step but has less Trotter error, and thus might require fewer Trotter steps to achive some target accuracy. This is referred to as the `SPLIT_OPERATOR` Trotter step in OpenFermion-Cirq.\n",
    "\n",
    "Thus, there are currently two options for simulating DiagonalCoulombHamiltonians, `LINEAR_SWAP_NETWORK` and `SPLIT_OPERATOR`, and they correspond to different orderings of the terms $H_j$ in the product formula. Different orderings give different results because the $H_j$ do not all commute. Let's construct a circuit with the `LINEAR_SWAP_NETWORK` method using just one first order Trotter step. We'll insert operations into the circuit using the strategy `EARLIEST` so the printed output will be most compact. Still, the circuit will be longer than the width of this notebook, so we'll print it out transposed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       1          2       3\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.02 XXYY────XXYY^-0.02\n",
      "│       │          │       │\n",
      "@───────@^0.0253   @───────@^0.0253\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "│       @──────────@^0.076 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.02 XXYY────XXYY^-0.02\n",
      "│       │          │       │\n",
      "@───────@^0.0253   @───────@^0.0253\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "Z^-0.04 │          │       Z^-0.04\n",
      "│       │          │       │\n",
      "│       @──────────@^0.076 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "│       Z^-0.04    Z^-0.04 │\n",
      "│       │          │       │\n",
      "│       @──────────@^0.076 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "@───────@^0.0253   @───────@^0.0253\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.02 XXYY────XXYY^-0.02\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "│       @──────────@^0.076 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "@───────@^0.0253   @───────@^0.0253\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.02 XXYY────XXYY^-0.02\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from openfermioncirq import trotter\n",
    "\n",
    "# Set algorithm parameters.\n",
    "time = 1.0\n",
    "n_steps = 1\n",
    "order = 1\n",
    "\n",
    "# Construct circuit\n",
    "swap_network_trotter_step = cirq.Circuit.from_ops(\n",
    "    openfermioncirq.simulate_trotter(\n",
    "        qubits, hamiltonian, time, n_steps, order,\n",
    "        algorithm=trotter.LINEAR_SWAP_NETWORK),\n",
    "    strategy=cirq.InsertStrategy.EARLIEST)\n",
    "\n",
    "# Print circuit.\n",
    "cirq.DropNegligible().optimize_circuit(swap_network_trotter_step)\n",
    "print(swap_network_trotter_step.to_text_diagram(transpose=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's do the same, but using the `SPLIT_OPERATOR` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       1             2           3\n",
      "│       │             │           │\n",
      "│       │             YXXY────────#2^0.5\n",
      "│       │             │           │\n",
      "│       YXXY──────────#2^0.608    │\n",
      "│       │             │           │\n",
      "│       │             YXXY────────#2^-0.00416\n",
      "│       │             │           │\n",
      "YXXY────#2^0.667      │           │\n",
      "│       │             │           │\n",
      "Z       YXXY──────────#2^0.612    │\n",
      "│       │             │           │\n",
      "│       │             YXXY────────#2^-0.498\n",
      "│       │             │           │\n",
      "Z       Z^-0.02       │           │\n",
      "│       │             │           │\n",
      "│       │             Z^-0.02     Z^-0.04\n",
      "│       │             │           │\n",
      "│       │             YXXY────────#2^0.498\n",
      "│       │             │           │\n",
      "│       YXXY──────────#2^-0.612   │\n",
      "│       │             │           │\n",
      "YXXY────#2^-0.667     │           │\n",
      "│       │             │           │\n",
      "│       │             YXXY────────#2^0.00416\n",
      "│       │             │           │\n",
      "│       YXXY──────────#2^-0.608   │\n",
      "│       │             │           │\n",
      "@───────@^0.0507      │           │\n",
      "│       │             │           │\n",
      "×───────×             YXXY────────#2^-0.5\n",
      "│       │             │           │\n",
      "│       │             @───────────@^0.0507\n",
      "│       │             │           │\n",
      "│       │             ×───────────×\n",
      "│       │             │           │\n",
      "│       @─────────────@^0.152     │\n",
      "│       │             │           │\n",
      "│       ×─────────────×           │\n",
      "│       │             │           │\n",
      "@───────@^0.0507      @───────────@^0.0507\n",
      "│       │             │           │\n",
      "×───────×             ×───────────×\n",
      "│       │             │           │\n",
      "│       @─────────────@^0.152     │\n",
      "│       │             │           │\n",
      "│       ×─────────────×           │\n",
      "│       │             │           │\n",
      "#2──────YXXY^0.5      │           │\n",
      "│       │             │           │\n",
      "│       #2────────────YXXY^0.608  │\n",
      "│       │             │           │\n",
      "#2──────YXXY^-0.00416 │           │\n",
      "│       │             │           │\n",
      "│       │             #2──────────YXXY^0.667\n",
      "│       │             │           │\n",
      "│       #2────────────YXXY^0.612  Z\n",
      "│       │             │           │\n",
      "#2──────YXXY^-0.498   │           │\n",
      "│       │             │           │\n",
      "│       │             Z^-0.02     Z\n",
      "│       │             │           │\n",
      "Z^-0.04 Z^-0.02       │           │\n",
      "│       │             │           │\n",
      "#2──────YXXY^0.498    │           │\n",
      "│       │             │           │\n",
      "│       #2────────────YXXY^-0.612 │\n",
      "│       │             │           │\n",
      "│       │             #2──────────YXXY^-0.667\n",
      "│       │             │           │\n",
      "#2──────YXXY^0.00416  │           │\n",
      "│       │             │           │\n",
      "│       #2────────────YXXY^-0.608 │\n",
      "│       │             │           │\n",
      "│       │             ×───────────×\n",
      "│       │             │           │\n",
      "#2──────YXXY^-0.5     │           │\n",
      "│       │             │           │\n",
      "×───────×             │           │\n",
      "│       │             │           │\n",
      "│       ×─────────────×           │\n",
      "│       │             │           │\n",
      "×───────×             ×───────────×\n",
      "│       │             │           │\n",
      "│       ×─────────────×           │\n",
      "│       │             │           │\n",
      "\n"
     ]
    }
   ],
   "source": [
    "split_operator_trotter_step = cirq.Circuit.from_ops(\n",
    "    openfermioncirq.simulate_trotter(\n",
    "        qubits, hamiltonian, time, n_steps, order,\n",
    "        algorithm=trotter.SPLIT_OPERATOR),\n",
    "    strategy=cirq.InsertStrategy.EARLIEST)\n",
    "cirq.DropNegligible().optimize_circuit(split_operator_trotter_step)\n",
    "print(split_operator_trotter_step.to_text_diagram(transpose=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's run these circuits on the simulator that comes with Cirq and compute the energy of the resulting states."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Energy of state obtained with swap network method: -0.19257496156625084\n",
      "Energy of state obtained with split-operator method: -0.1925763659706332\n"
     ]
    }
   ],
   "source": [
    "# Initialize Cirq simulator.\n",
    "simulator = cirq.google.XmonSimulator()\n",
    "\n",
    "# Convert the Hamiltonian to a sparse matrix.\n",
    "hamiltonian_sparse = openfermion.get_sparse_operator(hamiltonian)\n",
    "\n",
    "# Obtain initial state vector as integer.\n",
    "initial_state = sum(2 ** (n_qubits - 1 - i) for i in occupied_orbitals)\n",
    "\n",
    "# Construct and simulate circuit using the swap network method.\n",
    "circuit = state_preparation_circuit + swap_network_trotter_step\n",
    "result = simulator.simulate(circuit, initial_state=initial_state)\n",
    "final_state = result.final_state\n",
    "\n",
    "print('Energy of state obtained with swap network method: {}'.format(\n",
    "    openfermion.expectation(hamiltonian_sparse, final_state).real))\n",
    "\n",
    "# Construct and simulate circuit using the split-operator method.\n",
    "circuit = state_preparation_circuit + split_operator_trotter_step\n",
    "result = simulator.simulate(circuit, initial_state=initial_state)\n",
    "final_state = result.final_state\n",
    "\n",
    "print('Energy of state obtained with split-operator method: {}'.format(\n",
    "    openfermion.expectation(hamiltonian_sparse, final_state).real))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Increasing the number of Trotter steps will cause both methods to converge to the same operation, corresponding to an exact simulation. You can play around with the number of Trotter steps to confirm. Note that for NISQ applications one will often be interested in using the zeroth-order Trotter step, also known as the first-order asymmetric Trotter step. We can implement these Trotter steps by setting the order to zero, as we do below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       1          2       3\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.04 XXYY────XXYY^-0.04\n",
      "│       │          │       │\n",
      "@───────@^0.0507   @───────@^0.0507\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "│       @──────────@^0.152 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.04 XXYY────XXYY^-0.04\n",
      "│       │          │       │\n",
      "@───────@^0.0507   @───────@^0.0507\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "Z^-0.04 │          │       Z^-0.04\n",
      "│       │          │       │\n",
      "│       @──────────@^0.152 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "│       Z^-0.04    Z^-0.04 │\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Set algorithm parameters.\n",
    "time = 1.0\n",
    "n_steps = 1\n",
    "order = 0\n",
    "\n",
    "# Construct circuit\n",
    "swap_network_trotter_step = cirq.Circuit.from_ops(\n",
    "    openfermioncirq.simulate_trotter(\n",
    "        qubits, hamiltonian, time, n_steps, order,\n",
    "        algorithm=trotter.LINEAR_SWAP_NETWORK),\n",
    "    strategy=cirq.InsertStrategy.EARLIEST)\n",
    "cirq.DropNegligible().optimize_circuit(swap_network_trotter_step)\n",
    "print(swap_network_trotter_step.to_text_diagram(transpose=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the unusual pattern of fermionic swap networks towards the end. What is happening there is that in the zeroth order step of a `LINEAR_SWAP_NETWORK` style Trotter step, the qubit order is reversed upon output. To avoid this one needs to set an optional called `omit_final_swaps`, e.g."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       1          2       3\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.04 XXYY────XXYY^-0.04\n",
      "│       │          │       │\n",
      "@───────@^0.0507   @───────@^0.0507\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "│       @──────────@^0.152 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "XXYY────XXYY^-0.04 XXYY────XXYY^-0.04\n",
      "│       │          │       │\n",
      "@───────@^0.0507   @───────@^0.0507\n",
      "│       │          │       │\n",
      "×ᶠ──────×ᶠ         ×ᶠ──────×ᶠ\n",
      "│       │          │       │\n",
      "Z^-0.04 │          │       Z^-0.04\n",
      "│       │          │       │\n",
      "│       @──────────@^0.152 │\n",
      "│       │          │       │\n",
      "│       ×ᶠ─────────×ᶠ      │\n",
      "│       │          │       │\n",
      "│       Z^-0.04    Z^-0.04 │\n",
      "│       │          │       │\n",
      "\n"
     ]
    }
   ],
   "source": [
    "swap_network_trotter_step = cirq.Circuit.from_ops(\n",
    "    openfermioncirq.simulate_trotter(\n",
    "        qubits, hamiltonian, time, n_steps, order,\n",
    "        algorithm=trotter.LINEAR_SWAP_NETWORK,\n",
    "        omit_final_swaps=True),\n",
    "    strategy=cirq.InsertStrategy.EARLIEST)\n",
    "cirq.DropNegligible().optimize_circuit(swap_network_trotter_step)\n",
    "print(swap_network_trotter_step.to_text_diagram(transpose=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can also have fun compiling arbitrary high-order formulas. Here's the third-order symmetric formula:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0          1             2          3\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00982  │             │          Z^0.00982\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00982     Z^0.00982  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00982  │             │          Z^0.00982\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00982     Z^0.00982  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00816  │             │          Z^0.00816\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00816     Z^0.00816  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00816  │             │          Z^0.00816\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00816     Z^0.00816  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00648 XXYY───────XXYY^-0.00648\n",
      "│          │             │          │\n",
      "@──────────@^0.0082      @──────────@^0.0082\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0246   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00648 XXYY───────XXYY^-0.00648\n",
      "│          │             │          │\n",
      "@──────────@^0.0082      @──────────@^0.0082\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.013   │             │          Z^-0.013\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0246   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.013      Z^-0.013   │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0246   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.0082      @──────────@^0.0082\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00648 XXYY───────XXYY^-0.00648\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0246   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.0082      @──────────@^0.0082\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00648 XXYY───────XXYY^-0.00648\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00816  │             │          Z^0.00816\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00816     Z^0.00816  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00816  │             │          Z^0.00816\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00816     Z^0.00816  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0155  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00517    @──────────@^-0.00517\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00408  XXYY───────XXYY^0.00408\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00982  │             │          Z^0.00982\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00982     Z^0.00982  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^0.00982  │             │          Z^0.00982\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^0.00982     Z^0.00982  │\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^-0.0187  │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^-0.00622    @──────────@^-0.00622\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^0.00491  XXYY───────XXYY^0.00491\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "Z^-0.00619 │             │          Z^-0.00619\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "│          Z^-0.00619    Z^-0.00619 │\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "│          @─────────────@^0.0118   │\n",
      "│          │             │          │\n",
      "│          ×ᶠ────────────×ᶠ         │\n",
      "│          │             │          │\n",
      "@──────────@^0.00392     @──────────@^0.00392\n",
      "│          │             │          │\n",
      "XXYY───────XXYY^-0.00309 XXYY───────XXYY^-0.00309\n",
      "│          │             │          │\n",
      "×ᶠ─────────×ᶠ            ×ᶠ─────────×ᶠ\n",
      "│          │             │          │\n",
      "\n"
     ]
    }
   ],
   "source": [
    "order=3\n",
    "n_steps=1\n",
    "swap_network_trotter_step = cirq.Circuit.from_ops(\n",
    "    openfermioncirq.simulate_trotter(\n",
    "        qubits, hamiltonian, time, n_steps, order,\n",
    "        algorithm=trotter.LINEAR_SWAP_NETWORK),\n",
    "    strategy=cirq.InsertStrategy.EARLIEST)\n",
    "cirq.DropNegligible().optimize_circuit(swap_network_trotter_step)\n",
    "print(swap_network_trotter_step.to_text_diagram(transpose=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Application to phase estimation\n",
    "\n",
    "Phase estimation is a procedure that, given access to a controlled unitary and one of its eigenvectors, estimates the phase of the eigenvalue corresponding to that eigenvector. In the context of quantum simulation, this unitary is usually the time evolution operator $e^{-iHt}$. Thus if $H\\lvert n\\rangle = E_n \\lvert n \\rangle$, and we initialize the system in state $\\lvert n \\rangle$, phase estimation would estimate the value $E_n t / (2\\pi)$. To avoid aliasing of phases, $t$ should be chosen to be smaller than $2\\pi / \\lvert E_n \\rvert$. \n",
    "\n",
    "The simplest phase estimation circuit measures one bit of the phase in four steps:\n",
    "1. Perform a Hadamard transform on the control qubit.\n",
    "2. Apply the controlled unitary.\n",
    "3. Perform a Hadamard transform on the control qubit.\n",
    "4. Measure the control qubit.\n",
    "\n",
    "Below, we demonstrate the construction of this circuit where the controlled unitary is a controlled Trotter step of our jellium Hamiltonian. This circuit can be used as a building block of a larger phase estimation circuit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1 0         1            2         3\n",
      "│  │         │            │         │\n",
      "H  │         │            │         │\n",
      "│  │         │            │         │\n",
      "@──XXYY──────XXYY^-0.0484 │         │\n",
      "│  │         │            │         │\n",
      "@──@─────────@^0.0613     │         │\n",
      "│  │         │            │         │\n",
      "│  ×ᶠ────────×ᶠ           │         │\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────XXYY──────XXYY^-0.0484\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────@─────────@^0.0613\n",
      "│  │         │            │         │\n",
      "│  │         │            ×ᶠ────────×ᶠ\n",
      "│  │         │            │         │\n",
      "@──┼─────────@────────────@^0.184   │\n",
      "│  │         │            │         │\n",
      "│  │         ×ᶠ───────────×ᶠ        │\n",
      "│  │         │            │         │\n",
      "@──XXYY──────XXYY^-0.0484 │         │\n",
      "│  │         │            │         │\n",
      "@──@─────────@^0.0613     │         │\n",
      "│  │         │            │         │\n",
      "│  ×ᶠ────────×ᶠ           │         │\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────XXYY──────XXYY^-0.0484\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────@─────────@^0.0613\n",
      "│  │         │            │         │\n",
      "│  │         │            ×ᶠ────────×ᶠ\n",
      "│  │         │            │         │\n",
      "@──┼─────────@────────────@^0.184   │\n",
      "│  │         │            │         │\n",
      "│  │         ×ᶠ───────────×ᶠ        │\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────┼─────────@^-0.0968\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────@^-0.0968 │\n",
      "│  │         │            │         │\n",
      "@──┼─────────@^-0.0968    │         │\n",
      "│  │         │            │         │\n",
      "@──@^-0.0968 │            │         │\n",
      "│  │         │            │         │\n",
      "@──┼─────────@────────────@^0.184   │\n",
      "│  │         │            │         │\n",
      "│  │         ×ᶠ───────────×ᶠ        │\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────@─────────@^0.0613\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────XXYY──────XXYY^-0.0484\n",
      "│  │         │            │         │\n",
      "@──@─────────@^0.0613     ×ᶠ────────×ᶠ\n",
      "│  │         │            │         │\n",
      "@──XXYY──────XXYY^-0.0484 │         │\n",
      "│  │         │            │         │\n",
      "│  ×ᶠ────────×ᶠ           │         │\n",
      "│  │         │            │         │\n",
      "@──┼─────────@────────────@^0.184   │\n",
      "│  │         │            │         │\n",
      "│  │         ×ᶠ───────────×ᶠ        │\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────@─────────@^0.0613\n",
      "│  │         │            │         │\n",
      "@──┼─────────┼────────────XXYY──────XXYY^-0.0484\n",
      "│  │         │            │         │\n",
      "@──@─────────@^0.0613     ×ᶠ────────×ᶠ\n",
      "│  │         │            │         │\n",
      "@──XXYY──────XXYY^-0.0484 │         │\n",
      "│  │         │            │         │\n",
      "│  ×ᶠ────────×ᶠ           │         │\n",
      "│  │         │            │         │\n",
      "H  │         │            │         │\n",
      "│  │         │            │         │\n",
      "M  │         │            │         │\n",
      "│  │         │            │         │\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Define a phase estimation circuit.\n",
    "def measure_bit_of_phase(system_qubits,\n",
    "                         control_qubit,\n",
    "                         controlled_unitary):\n",
    "    yield cirq.H(control_qubit)\n",
    "    yield controlled_unitary\n",
    "    yield cirq.H(control_qubit)\n",
    "    yield cirq.measure(control_qubit)\n",
    "\n",
    "# Get an upper bound on the Hamiltonian norm.\n",
    "import numpy\n",
    "bound = numpy.sum(numpy.abs(hamiltonian.one_body)) + numpy.sum(numpy.abs(hamiltonian.two_body))\n",
    "\n",
    "# Construct phase estimation circuit.\n",
    "time = 2 * numpy.pi / bound\n",
    "control = cirq.LineQubit(-1)\n",
    "\n",
    "controlled_unitary = openfermioncirq.simulate_trotter(\n",
    "    qubits, hamiltonian, time,\n",
    "    n_steps=1,\n",
    "    order=1,\n",
    "    algorithm=trotter.LINEAR_SWAP_NETWORK,\n",
    "    control_qubit=control)\n",
    "\n",
    "circuit = cirq.Circuit.from_ops(\n",
    "    measure_bit_of_phase(\n",
    "        qubits,\n",
    "        control,\n",
    "        controlled_unitary))\n",
    "\n",
    "# Print the circuit.\n",
    "cirq.DropNegligible().optimize_circuit(circuit)\n",
    "print(circuit.to_text_diagram(transpose=True))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
